package br.edu.ufcg.greengrid.event.reader;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import br.edu.ufcg.greengrid.event.Event;
import br.edu.ufcg.greengrid.event.JobSubmissionEvent;

public class JobSubmissionEventReaderTest {

	private JobSubmissionEventReader reader;

	@Before
	public void setUp() {

		this.reader = new JobSubmissionEventReader();
	}

	@Test
	public void testGetEvent() {

		JobSubmissionEvent expected = new JobSubmissionEvent("id1", 1, 10);
		JobSubmissionEvent actual = reader.getEvent("id1" + Event.SEPARATOR + "1" + Event.SEPARATOR + "10");
		assertEquals(expected, actual);

	}

	@Test
	public void testGetNormalizedDelta() {

		assertEquals(0L, reader.getNormalizedDelta(1L, null, 1L));
		assertEquals(1L, reader.getNormalizedDelta(1L, 0L, 0L));
		assertEquals(-1L, reader.getNormalizedDelta(1L, 0L, 2L));

		assertEquals(0L, reader.getNormalizedDelta(1L, null, 1L));
		assertEquals(2L, reader.getNormalizedDelta(1L, 1L, 0L));
		assertEquals(0L, reader.getNormalizedDelta(1L, 1L, 2L));

		assertEquals(0L, reader.getNormalizedDelta(1L, null, 1L));
		assertEquals(0L, reader.getNormalizedDelta(1L, -1L, 0L));
		assertEquals(-2L, reader.getNormalizedDelta(1L, -1L, 2L));

	}

	@Test
	public void testFilterEvents() {

		JobSubmissionEvent event = null;

		event = new JobSubmissionEvent("id1", 0L, 0L);
		assertFalse(reader.run(event));

		event = new JobSubmissionEvent("id2", 0L, 1L);
		assertTrue(reader.run(event));

		event = new JobSubmissionEvent("id3", 1L, 0L);
		assertFalse(reader.run(event));

		event = new JobSubmissionEvent("id4", 0L, -1L);
		assertFalse(reader.run(event));

		event = new JobSubmissionEvent("id5", 1L, -1L);
		assertFalse(reader.run(event));

	}
}
